package ru.cybertek.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

//import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
//import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import org.springframework.jdbc.core.simple.*;

import ru.cybertek.dao.PersonDao;
import ru.cybertek.dao.impl.jdbc.AddPersonSqlQuery;
import ru.cybertek.dao.impl.jdbc.DeletePersonSqlQuery;
import ru.cybertek.dao.impl.jdbc.SelectPersonByIdSqlQuery;
import ru.cybertek.dao.impl.jdbc.UpdatePersonSqlQuery;
import ru.cybertek.domain.Account;
import ru.cybertek.domain.Person;

/**
 * @author Dmitry Demyankov
 * @version $Revision: 17 $
 * 
 *          (C) Copyright 2009 CyberTek Ltd. All rights reserved.
 */
public class PersonDaoImpl extends SimpleJdbcDaoSupport implements PersonDao
{
    public static final int PERSON_ID_COLUMN_INDEX = 1;

    public static final int PERSON_FIRST_NAME_COLUMN_INDEX = 2;

    public static final int PERSON_MIDDLE_NAME_COLUMN_INDEX = 3;

    public static final int PERSON_LAST_NAME_COLUMN_INDEX = 4;

    public static final String SELECT_PERSON_LIST_SQL_QUERY = "SELECT * FROM person";

    private static class PersonRowMapper implements
        ParameterizedRowMapper<Person>
    {
        public Person mapRow(ResultSet resultSet, int rowNum)
            throws SQLException
        {
            Person person = new Person();

            person.setId(resultSet.getInt(PERSON_ID_COLUMN_INDEX));
            person.setFirstName(resultSet
                .getString(PERSON_FIRST_NAME_COLUMN_INDEX));
            person.setMiddleName(resultSet
                .getString(PERSON_MIDDLE_NAME_COLUMN_INDEX));
            person.setLastName(resultSet
                .getString(PERSON_LAST_NAME_COLUMN_INDEX));

            return person;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see ru.cybertek.dao.PersonDao#getPerson(java.lang.Integer)
     */
    public Person getPerson(Integer id)
    {
        SelectPersonByIdSqlQuery query = new SelectPersonByIdSqlQuery(
            getDataSource());
        return query.execute(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see ru.cybertek.dao.PersonDao#getPersonAccounts(java.lang.Integer)
     */
    public List<Account> getPersonAccounts(Integer personId)
    {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see ru.cybertek.dao.PersonDao#getPersonList()
     */
    public List<Person> getPersonList()
    {
        return getSimpleJdbcTemplate().query(SELECT_PERSON_LIST_SQL_QUERY,
            new PersonDaoImpl.PersonRowMapper(), Collections.EMPTY_MAP);
    }

    /*
     * (non-Javadoc)
     * 
     * @see ru.cybertek.dao.PersonDao#savePerson(ru.cybertek.domain.Person)
     */
    public void savePerson(Person person)
    {
        if (person == null)
            return;

        if (person.getId() == null)
        {
            AddPersonSqlQuery query = new AddPersonSqlQuery(getDataSource());
            query.update(person);
        } else
        {
            UpdatePersonSqlQuery query = new UpdatePersonSqlQuery(
                getDataSource());
            query.update(person);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see ru.cybertek.dao.PersonDao#deletePerson(ru.cybertek.domain.Person)
     */
    public void deletePerson(Person person)
    {
        if (person == null)
            return;

        DeletePersonSqlQuery query = new DeletePersonSqlQuery(getDataSource());
        query.execute(person);
    }
}
